﻿using Microsoft.AspNetCore.Mvc;
using RTW.Core.Entity;
using RTW.Core.Enums;
using RTW.Core.Extensions;
using RTW.Models;
using RTW.Servers;
using RTW.Web.Areas.Query.Models;
using RTW.Web.Comm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;

namespace RTW.Web.Areas.Query.Controllers
{
    [Area("Query")]
    public class JHWenShuController : CommController
    {

        TFilesServer filesServer;
        public JHWenShuController()
        {
            filesServer = new TFilesServer();
        }

        public IActionResult Index()
        {
            return View();
        }


        public async Task<IActionResult> GetJHWenShuJsonData(string filesQueryJson)
        {
            //查询条件转查询对像
            JHWenShuQueryM query = null;
            if (!string.IsNullOrEmpty(filesQueryJson))
            {
                try
                {
                    query = Newtonsoft.Json.JsonConvert.DeserializeObject<JHWenShuQueryM>(filesQueryJson);
                }
                catch (Exception ex)
                {
                    
                }
            }
            //查询条件
            Expression<Func<TFiles, bool>> predicate = ExpressionBuilder.True<TFiles>();

            predicate = predicate.And(b => b.FileType == 2);
            predicate = predicate.And(b => b.Flag1 == 2);
            predicate = predicate.And(b => b.DMID == UserDMID);

            if (!string.IsNullOrEmpty(query.FileNo))
            {
                predicate = predicate.And(b => b.FileNo.Contains(query.FileNo));
            }
            if (!string.IsNullOrEmpty(query.FType))
            {
                predicate = predicate.And(b => b.FType==query.FType);
            }
            if (!string.IsNullOrEmpty(query.FYear))
            {
                predicate = predicate.And(b => int.Parse(b.FYear)>= int.Parse(query.FYear));
            }
            if (!string.IsNullOrEmpty(query.FYear2))
            {
                predicate = predicate.And(b => int.Parse(b.FYear) <= int.Parse(query.FYear2));
            }
            if (!string.IsNullOrEmpty(query.FTerm))
            {
                predicate = predicate.And(b => b.FTerm==query.FTerm);
            }
            if (!string.IsNullOrEmpty(query.ZRZ))
            {
                predicate = predicate.And(b => b.CV.ResponsiblePerson.Contains(query.ZRZ));
            }
            if (!string.IsNullOrEmpty(query.WH))
            {
                predicate = predicate.And(b => b.CV.ArticleNo.Contains(query.WH));
            }
            if (!string.IsNullOrEmpty(query.AJTM))
            {
                predicate = predicate.And(b => b.CV.Title.Contains(query.AJTM));
            }
            if (!string.IsNullOrEmpty(query.search))
            {
                //string.Concat只支持四参数，更多参数需连接使用。
                predicate = predicate.And(b => string.Concat(string.Concat(b.FileNo, b.FType, b.FYear, b.FTerm), string.Concat(b.CV.ResponsiblePerson, b.CV.ArticleNo, b.CV.Title)).Contains(query.search));
            }

            //分页
            PageInfo pageinfo = (PageInfo)query;
            //默认排序
            //a.FYear desc,a.FTerm asc,a.FType asc,right('000000'+ltrim(a.BookNum),7) asc
            List<SortInfo<TFiles, object>> sortInfos=   new List<SortInfo<TFiles, object>> {
                   new SortInfo<TFiles, object>{  Orderby=o=>o.FYear, SortMethods = SortEnum.desc },
                   new SortInfo<TFiles, object>{  Orderby=o=>o.FTerm, SortMethods = SortEnum.asc },
                   new SortInfo<TFiles, object>{  Orderby=o=>o.FType, SortMethods = SortEnum.asc },
                   new SortInfo<TFiles, object>{  Orderby=o=>o.BookNum, SortMethods = SortEnum.asc }
            };
            //排序
            if (!string.IsNullOrEmpty(pageinfo.Sort))
            {
                sortInfos.Clear();
                try
                {
                    // var sort = ExpressionBuilder.GetPropertyInfo(typeof(TFiles), pageinfo.Sort);
                    //PropertyDescriptor prop = TypeDescriptor.GetProperties(typeof(TFiles)).Find(pageinfo.Sort);
                    var linq = ExpressionBuilder.CreateExpression<TFiles, object>(pageinfo.Sort);
                    //PropertyInfo prop = typeof(TFiles).GetProperty(pageinfo.Sort);
                    var forder = string.IsNullOrEmpty(pageinfo.Order)|| pageinfo.Order=="null" ? SortEnum.asc : (SortEnum)Enum.Parse(typeof(SortEnum), pageinfo.Order);
                    sortInfos.Add(new SortInfo<TFiles, object> { Orderby = linq, SortMethods = forder });
                }
                catch (Exception ex)
                {
                    
                }
                
            }

            var list = await filesServer.GetJHWenShuList(predicate, sortInfos, pageinfo);

            //var list = await filesServer.GetAsync(m => m.DMID == DMID && m.Flag2 == 1); //string.Format("select * from TOrganization where DMID='{0}' and Flag2=1 order by OrgDepartment asc,Flag1 asc,AddTime asc;", DMID);
            return Json(new { total = list.total, rows = list.rows, search= query.search });
            try
            {
                //ArticleContent query = null;
                //if (!string.IsNullOrEmpty(seniorQueryJson))
                //{
                //    query = Newtonsoft.Json.JsonConvert.DeserializeObject<ArticleContent>(seniorQueryJson);
                //}
                //System.Linq.Expressions.Expression<Func<ArticleContent, bool>> predicate = ExpressionBuilder.True<ArticleContent>();
                //predicate = predicate.And(b => b.Id > 0);

                //if (searchContent != null)
                //{
                //    predicate = predicate.And(b => b.Title.IndexOf(searchContent) != -1);
                //}
                //if (query != null)
                //{
                //    if (query.TypeID != null)
                //    {
                //        predicate = predicate.And(b => b.TypeID == query.TypeID);
                //    }
                //}

                //PageInfo pageinfo = new PageInfo { };
                //(List<ArticleContent> list, long count) datas = await ArticleContentDAL.QueryAsync(predicate, null, pageinfo);

                //var lists = datas.list;
                //return lists.GetJson<ArticleContent>(sidx, sord, page, limit, SysTool.GetPropertyNameArray<ArticleContent>());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        //public async Task<IActionResult> List(string searchContent, string seniorQueryJson, int page = 1, int limit = 10, string sidx = "CreateDt", string sord = "desc")
        //{
        //public async Task<IActionResult> GetJHWenShuJsonData(string filesQueryJson)
        //{
        //    TFiles query = null;
        //    if (!string.IsNullOrEmpty(filesQueryJson))
        //    {
        //        query = Newtonsoft.Json.JsonConvert.DeserializeObject<TFiles>(filesQueryJson);
        //    }
        //    Expression<Func<TFiles, bool>> predicate = ExpressionBuilder.True<TFiles>();

        //    predicate = predicate.And(b => b.FileType == 2);
        //    predicate = predicate.And(b => b.Flag1 == 2);

        //    predicate = predicate.And(b => b.DMID == UserDMID);
        //    PageInfo pageinfo = new PageInfo { };
        //    //a.FYear desc,a.FTerm asc,a.FType asc,right('000000'+ltrim(a.BookNum),7) asc
        //    List<SortInfo<TFiles, object>> sortInfos = new List<SortInfo<TFiles, object>> {
        //           new SortInfo<TFiles, object>{  Orderby=o=>o.FYear, SortMethods = SortEnum.Desc },
        //           new SortInfo<TFiles, object>{  Orderby=o=>o.FTerm, SortMethods = SortEnum.Asc },
        //           new SortInfo<TFiles, object>{  Orderby=o=>o.FType, SortMethods = SortEnum.Asc },
        //           new SortInfo<TFiles, object>{  Orderby=o=>o.BookNum, SortMethods = SortEnum.Asc }
        //    };
        //    var list = await filesServer.GetJHWenShuList(predicate, sortInfos, pageinfo);

        //    //var list = await filesServer.GetAsync(m => m.DMID == DMID && m.Flag2 == 1); //string.Format("select * from TOrganization where DMID='{0}' and Flag2=1 order by OrgDepartment asc,Flag1 asc,AddTime asc;", DMID);
        //    return Json(new { total = list.total, rows = list.rows });
            //}

        }
}
